package cn.com.yx.ms.service.impl;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.domain.dbmodel.CommandCount;
import cn.com.yx.ms.domain.sta.OnOfflineCountModel;
import cn.com.yx.ms.domain.sta.TrafficCountModel;
import cn.com.yx.ms.domain.statistics.StaChartDevInfo;
import cn.com.yx.ms.persistence.CmdRecordMapper;
import cn.com.yx.ms.persistence.DevEquStoreMapper;
import cn.com.yx.ms.persistence.TestMapper;
import cn.com.yx.ms.service.IStaService;
import cn.com.yx.ms.service.ITestService;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ApphubServiceImpl
 */
@Service
@Transactional
public class StaServiceImpl implements IStaService {

    @Resource
    private DevEquStoreMapper devEquStoreMapper;

    @Resource
    private CmdRecordMapper cmdRecordMapper;

    @Override
    public StaChartDevInfo staChart1(){
        return devEquStoreMapper.staIndexChart1();
    }

    @Override
    public ApiResult staCommand() {
        ApiResult apiResult = new ApiResult();
        Map<String,int[]> map = new HashMap<String,int[]>();

        List<CommandCount> upcmd = cmdRecordMapper.selectCommandCount("request");
        int[] upcmds = new int[24];

        List<CommandCount> downcmd = cmdRecordMapper.selectCommandCount("response");
        int[] downcmds = new int[24];


        for(int i=0;i<24;i++){
            upcmds[i] = 0;
            downcmds[i] = 0;
            String str = String.format("%02d", i);
            for(CommandCount commandCount : upcmd) {
                if(str.equals(commandCount.getCmdtime())){
                    // 上行数据添加
                    upcmds[i] = commandCount.getCount();
                    break;
                }
            }

            for(CommandCount commandCount : downcmd) {
                if(str.equals(commandCount.getCmdtime())){
                    // 下行数据添加
                    downcmds[i] = commandCount.getCount();
                    break;
                }
            }
        }
        map.put("upcmd",upcmds);
        map.put("downcmd",downcmds);

        apiResult.setData(map);
        apiResult.setSucc(true);
        return apiResult;
    }

    /**
     * 心跳指令报表
     * @return
     */
    @Override
    public ApiResult staCommand3003() {
        ApiResult apiResult = new ApiResult();
        List<CommandCount> list = cmdRecordMapper.selectCommand3003Count();
        Map<String,List<Object>> map  = new HashMap<String,List<Object>>();

        List<Object> dateList = new ArrayList<Object>();
        List<Object> dataList = new ArrayList<Object>();

        if(null != list && list.size() > 0){
            for(CommandCount commandCount : list){
                dateList.add(commandCount.getCmdtime());
                dataList.add(commandCount.getCount());
            }


            apiResult.setSucc(true);
        } else {
            apiResult.setSucc(false);
        }

        map.put("dateList",dateList);
        map.put("dataList",dataList);

        apiResult.setData(map);
        return apiResult;
    }

    /**
     * 设备总量
     * @return
     */
    @Override
    public ApiResult staCommandTotalDevice() {
        ApiResult apiResult = new ApiResult();
        List<Integer> list = cmdRecordMapper.selectCommand3TotalDevice();
        apiResult.setData(list);
        return apiResult;
    }

    /**
     * 设备在线离线
     * @return
     */
    @Override
    public ApiResult commandOnLineOffLineDevice() {
        ApiResult apiResult = new ApiResult();
        List<OnOfflineCountModel> list = cmdRecordMapper.selectCommandOnLineOffLineDevice();
        apiResult.setData(list);
        return apiResult;
    }

    /**
     * 4G流量top5
     * @return
     */
    @Override
    public ApiResult commandTraffic4g() {
        ApiResult apiResult = new ApiResult();
        List<TrafficCountModel> list4g = cmdRecordMapper.selectCommandTraffic("4g");

        // 流量值
        int[] netsize = new int[list4g.size()];

        // 站台名
        String[] name = new String[list4g.size()];

        // 返回值
        List<Object> trafficVal = new ArrayList<Object>();

        for(int i = 0; i<list4g.size();i++) {
            TrafficCountModel trafficCountModel = list4g.get(i);
            netsize[i] = trafficCountModel.getCmdbytelength();
            name[i] = trafficCountModel.getSname();
        }
        trafficVal.add(netsize);
        trafficVal.add(name);
        apiResult.setData(trafficVal);

        return apiResult;
    }

    /**
     * WIFITop5
     * @return
     */
    @Override
    public ApiResult commandTrafficWifi() {
        ApiResult apiResult = new ApiResult();
        List<TrafficCountModel> list4g = cmdRecordMapper.selectCommandTraffic("wifi");

        // 流量值
        int[] netsize = new int[list4g.size()];

        // 站台名
        String[] name = new String[list4g.size()];

        // 返回值
        List<Object> trafficVal = new ArrayList<Object>();

        for(int i = 0; i<list4g.size();i++) {
            TrafficCountModel trafficCountModel = list4g.get(i);
            netsize[i] = trafficCountModel.getCmdbytelength();
            name[i] = trafficCountModel.getSname();
        }
        trafficVal.add(netsize);
        trafficVal.add(name);
        apiResult.setData(trafficVal);

        return apiResult;
    }

    /**
     * 公交线路小车信息取得
     * @return
     */
    @Override
    public ApiResult staCommand5003() {
        ApiResult apiResult = new ApiResult();
        Map<String,int[]> map = new HashMap<String,int[]>();

        List<CommandCount> upcmd = cmdRecordMapper.selectCommand5003Count();
        int[] upcmds = new int[24];

        for(int i=0;i<24;i++){
            upcmds[i] = 0;
            String str = String.format("%02d", i);
            for(CommandCount commandCount : upcmd) {
                if(str.equals(commandCount.getCmdtime())){
                    // 上行数据添加
                    upcmds[i] = commandCount.getCount();
                    break;
                }
            }
        }
        map.put("upcmd",upcmds);
        apiResult.setData(map);
        apiResult.setSucc(true);
        return apiResult;
    }
}